/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  filename:   filemanagement.h/filemanagement
 *  copyright:  changhong electric co., ltd
 *  breif:      定义基本接口
 *  ------------------------------------------------------------------------------
 *  Rev     Date        Author          Comments
 *  1       2009-10-10  qingsong.li     Create
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef FILEMANAGEMENT_H_
#define FILEMANAGEMENT_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <locale.h>
#include <errno.h>
#include <stdarg.h>

#define LIST_SIZE       (20)                    /* 每次开辟20个内存空间 */

/* define file types */
#define TYPE_ERR        (0x0000)                /* 没有权限或是文件不存在 */
#define TYPE_MUS        (0x0001)                /* 音乐文件 */
#define TYPE_MOV        (0x0002)                /* 视频文件 */
#define TYPE_PIC        (0x0004)                /* 图片文件 */
#define TYPE_TXT        (0x0008)                /* 文本文件 */
#define TYPE_OTH        (0x0010)                /* 其他文件 */
#define TYPE_SL         (0x0100)                /* 软链接文件(夹)，本程序依文件处理，即不跟随 */
#define TYPE_F          (0x011F)                /* 所有文件 */
#define TYPE_D          (0x1000)                /* 文件夹 */
#define TYPE_ALL        (0x111F)                /* 所有文件和文件夹 */

/* answer to cover function */
#define CANCELALL       (0x0001)
#define SKIPALL         (0x0010)
#define REPLACEALL      (0x0100)
#define MERGEALL        (0x1000)

/* define the action type*/
#define ACT_DEL         (0)
#define ACT_CUT         (1)
#define ACT_CPY         (2)

/* Linear list structure */
typedef struct _PathList
{
        char (*pathname)[PATH_MAX];
        int length;
        int listsize;
} PathList;

/* Callback functions*/
typedef int callback_float(float);
typedef int callback_str(char*);

/* global variables */
int SHOWHIDE; /* 是否显示隐藏文件 */
int64_t TOTSIZE; /* 文件(夹)总大小 */
int64_t COPIEDSIZE; /* 已拷贝文件（夹）大小 */
int COVERGFLAG; /* 含ALL的文件覆盖信息 */

/* error handle functions */
void err_msg(const char *fmt, ...);
void err_doit(int errnoflag, int error, const char *fmt, va_list ap);

/* file type */
int path_exist(char *pathname);
int mus_file(char *pathname);
int mov_file(char *pathname);
int pic_file(char *pathname);
int txt_file(char *pathname);
int lnk_file(char *pathname);
int file_type(char *pathname);

/* path location */
int on_same_device(char *pathsrc, char *pathdest);
int subdir(char *subdir, char *pardir);
int pathvalid(PathList *list, char *pathdest, int act);

/* linear list interface */
int init_list(PathList *list);
int destruct_list(PathList *list);
int insert_list(PathList *list, char *entry);
int comb_list(PathList *listdest, PathList *listsrc);
int getentry_list(char *pathname, PathList *list, int type, int gethide,
                int getrecurv);

/* assist functions of the global interfaces */
int64_t get_file_size(char *pathname);
int64_t get_dir_size(char *pathname, int type, int calhide, int dorecurv);
int checkcover(char *pathdest, callback_str *askcover);
int delpath(char *pathname);
int64_t copyfile(char *pathsrc, char *pathdest, callback_str *askcover,
                callback_float *showprogress);
int64_t copydir(char *pathsrc, char *pathdest, callback_str *askcover,
                callback_float *showprogress);
int64_t cutfile(char *pathsrc, char *pathdest, callback_str *askcover,
                callback_float *showprogress);
int64_t cutdir(char *pathsrc, char *pathdest, callback_str *askcover,
                callback_float *showprogress);

/* global interfaces of the module */
int dolist(PathList *list, char *pathdest, int act, callback_str *askcover,
                callback_float *showprogress);
int get_cwd_entry(PathList *list);
#endif /* FILEMANAGEMENT_H_ */
